﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;

namespace Meraz.Core.Repositories
{
    public interface IMemberRepository : IRepository<Member, int>
    {
        Member GetByUserName(string applicationName, string userName);
        Member GetByUserKey(string applicationName, string providerUserKey);
        Member GetByEmail(string applicationName, string email);
        List<Member> GetByApplicationName(string applicationName);
    }
    public class MemberRepository : EFRepositoryBase<Member, int>, IMemberRepository
    {
        public MemberRepository() : base() {}
        public MemberRepository(Data.IMerazContext context) : base(context) { }
        public override Member GetById(int id)
        {
            return this.context.Members.Where(where1 => where1.ID == id).FirstOrDefault();
        }
        public Member GetByUserName(string applicationName, string userName)
        {
            return this.context.Members.Where(where1 => where1.UserName == userName && where1.ApplicationName == applicationName).FirstOrDefault();
        }
        public Member GetByUserKey(string applicationName, string providerUserKey)
        {
            return this.context.Members.Where(where1 => where1.ProviderUserKey == providerUserKey && where1.ApplicationName == applicationName).FirstOrDefault();
        }
        public Member GetByEmail(string applicationName, string email)
        {
            return this.context.Members.Where(where1 => where1.Email == email && where1.ApplicationName == applicationName).FirstOrDefault();
        }

        public List<Member> GetByApplicationName(string applicationName)
        {
            return this.context.Members.Where(where1 => where1.ApplicationName == applicationName).ToList();
        }

        public override void Add(Member entity)
        {
            this.context.AddToMembers(entity);
        }

        public override void Delete(Member entity)
        {
            this.context.Members.DeleteObject(entity);
        }

        public override List<Member> All()
        {
            return this.context.Members.ToList();
        }
    }
}
